using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


using SypEngine;
using SypConst.Garbage;
using SypEngine.SiteConfig;



namespace SypConst.Path
{

    public enum SypPathMode : ushort
    {
       DESIGN = 2,
       LOCAL = 1,
       ONLINE = 0
    }
    
    public enum SypPathRef : ushort
    {

        CATEGORIES_FOLDER = 49,
        BO_PAGE = 48,
        ADMIN_STATIC_FOLDER = 47,

        XSLT_FILE_PATH = 46,
        XSLT_BACK_TO_ALBUM = 45,
        XSLT_PREV_NEXT_PICTURE_NAME = 44,
        XSLT_CONTACT_LNK = 43,
        XSLT_THUMBNAIL_LNK = 42,
        XSLT_ALBUM_LNK = 41,
        XSLT_ALBUM_CURRENT_LNK = 40,
        XSLT_PAGENAME_TYPE_0 = 39,
        XSLT_PAGENAME_TYPE_1 = 38,
        XSLT_PATH_JAVASCRIPT = 37,
        XSLT_PATH_STYLESHEET = 36,
        XSLT_ARCHIVE_LNK = 35,
        XSLT_ABOUT_LNK = 34,
        XSLT_PATH_PICTURE = 33,
        XSLT_PATH_THUMBNAIL = 32,
        XSLT_PAGE = 31,
        XSLT_PAGE_RELATIF = 50,
        
        SKIN_THUMBNAIL_NAME = 30,
        SKIN_THUMBNAIL_PATH = 29,
        DESIGN_FOLDER = 28,
        TMP_FOLDER = 27,
        SITE_CONF_FILTER = 26,
        SITE_CONF_FOLDER = 25,
        ALBUM_PAGE = 24,
        SITE_CONF = 23,
        ZIP_FILE = 22,
        ADMIN_IMAGE_FOLDER = 21,
        ADMIN_XSLT_FOLDER = 20,
        ADMIN_STYLESHEET_FOLDER = 19,
        ADMIN_JAVASCRIPT_FOLDER = 18,
        IMAGE_FOLDER = 17,
        XSLT_FOLDER = 16,
        STYLESHEET_FOLDER = 15,
        JAVASCRIPT_FOLDER = 14,
        ROOT_FOLDER = 13,
        SITE_FOLDER = 12,
        THUMBNAIL_FILE = 11,
        PICTURE_FILE = 10,
        SYNC_FILE = 9,
        PENDING_ONLINE_FILE = 8,
        PENDING_LOCAL_FILE = 4,
        INDEX_PAGE = 7,
        PICTURE_FOLDER = 3,
        THUMBNAIL_FOLDER = 2,
        ZIP_FOLDER = 1,
        TOUP_FOLDER = 0
    }

    public class SypPathHandler
    {
        public static string CleanDirectoryName(string name)
        {

            if (name[0].Equals('/') && name.Length > 0)
            {
                name = name.Substring(1, name.Length);
            }
            if (name[name.Length - 1].Equals('/'))
            {
                name = name.Substring(0, name.Length - 1);
            }

            return name;
        }


        #region Path constant

        //prefix files
        public const string THUMBNAIL_PREF = "thumbnail";
        public const string PICTURE_PREF = "picture";
        public const string ALBUM_PAGE_PREF = "pictureList";
        public const string ALBUM_PREF_DIR = "album";
        public const string CATEGORY_PREF_DIR = "category";
        //Extension
        public const string CONF_EXTENSION = ".xml";
        public const string XML_EXTENSION = ".xml";
        public const string PICTURE_EXTENSION = ".jpg";
        public const string THUMBNAIL_EXTENSION = ".jpg";
        public const string LOG_EXTENSION = ".log";
        public const string INI_EXTENSION = ".ini";
        public const string PAGE_EXTENSION = ".html";
        public const string PAGE_STATIC_EXTENSION = ".html";
        public const string ZIP_EXTENSION = ".zip";
        //design folders
        public const string ADMIN_FOLDER = "admin/";
        //Local folders
        public const string DESIGN_FOLDER = @"design/";
        public const string CATEGORIES_FOLDER = @"categories/";
        public const string SITES_CONF = @"conf/";
        public const string LOG_FOLDER = @"logs/";
        public const string TMP_FOLDER = @"tmp/";
        public const string STATIC_FOLDER = @"static/";
        //site folders
        public const string LOCAL_FOLDER = "local/";
        public const string TO_UP_FOLDER = "toUp/";
        public const string ZIP_FOLDER = "zip/";
        //page index
        public const string ABOUT_PAGE_INDEX = "about.html";
        public const string ARCHIVES_PAGE_INDEX = "about.html";
        public const string XSLT_PAGE = "pagelayout.xsl";
        //page folders
        public const string STYLESHEET_FOLDER = "stylesheets/";
        public const string IMAGES_FOLDER = "images/";
        public const string JAVASCRIPT_FOLDER = "javascript/";
        public const string XSLT_FOLDER = "xslt/";
        //Generated Folders;
        public const string THUMBNAILS_FOLDER = "thumbnails/";
        public const string PICTURES_FOLDER = "pictures/";
        public const string PAGE_FOLDER = "categories/";
        //File index
        //private const string BO_PAGE = "bo.html";
        public const string BO_PAGE = "index.html";
        public const string DEFAULT_PAGE = "index.html";
        //sync xml file
        public const string SYNC_FILE = "syncActions.xml";
        //pending action file
        public const string PENDING_LOCAL_FILE = "pendingLocalActions.xml";
        public const string PENDING_ONLINE_FILE = "pendingOnlineActions.xml";
        //Skin Thumbnail name
        public const string SKIN_THUMBNAIL = "thumbnail.gif";

        #endregion


        #region static secondary methods

        public static void CreateLocalFolders()
        {
            if (!Directory.Exists(TMP_FOLDER))
                Directory.CreateDirectory(TMP_FOLDER);
            if (!Directory.Exists(SITES_CONF))
                Directory.CreateDirectory(SITES_CONF);
            if (!Directory.Exists(LOG_FOLDER))
                Directory.CreateDirectory(LOG_FOLDER);
            if (!Directory.Exists(DESIGN_FOLDER))
                Directory.CreateDirectory(DESIGN_FOLDER);
        }

        public static bool CheckForInitializing(string design)
        {
            if (!Directory.Exists(TMP_FOLDER))
                return false;
       
            if (!Directory.Exists(DESIGN_FOLDER + design))
                return false;
            
            if (!Directory.Exists(DESIGN_FOLDER + design + "/" + ADMIN_FOLDER))
                return false;
            
            if (!Directory.Exists(DESIGN_FOLDER + design + "/" + IMAGES_FOLDER))
                return false;
            
            if (!Directory.Exists(DESIGN_FOLDER + design + "/" + JAVASCRIPT_FOLDER))
                return false;

            if (!Directory.Exists(DESIGN_FOLDER + design + "/" + STYLESHEET_FOLDER))
                return false;

            if (!Directory.Exists(DESIGN_FOLDER + design + "/" + XSLT_FOLDER))
                return false;
            

            return true;
        }

        #endregion

        public static string GetPath(SypPathMode Mode, SypPathRef Ref, string design, params int[] Ids)
        {
            int SiteID = -1;
            int CategoryID = -1;
            int AlbumID = -1;
            int PictureID = -1;

            if (Ids != null)
            {
                for (int i = 0; i < Ids.Length; i++)
                {
                    switch (i)
                    {
                        case 0: SiteID = Ids[i]; break;
                        case 1: CategoryID = Ids[i]; break;
                        case 2: AlbumID = Ids[i]; break;
                        case 3: PictureID = Ids[i]; break;
                    }
                }
            }


            switch (Mode)
            {
                case SypPathMode.LOCAL:
                    {
                        switch (Ref)
                        {
                            case SypPathRef.THUMBNAIL_FILE: return (SiteID == -1 || PictureID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + THUMBNAILS_FOLDER + THUMBNAIL_PREF + PictureID + THUMBNAIL_EXTENSION;
                            case SypPathRef.PICTURE_FILE: return (SiteID == -1 || PictureID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER +PICTURES_FOLDER + PICTURE_PREF + PictureID + PICTURE_EXTENSION;
                            //case SypPathRef.CATEGORY_FOLDER: return (SiteID == -1 || CategoryID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/";
                            case SypPathRef.CATEGORIES_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER;
                            case SypPathRef.INDEX_PAGE: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + DEFAULT_PAGE;
                            case SypPathRef.BO_PAGE: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + BO_PAGE;
                            case SypPathRef.PENDING_ONLINE_FILE: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + PENDING_ONLINE_FILE;
                            case SypPathRef.PENDING_LOCAL_FILE: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + PENDING_LOCAL_FILE;
                            case SypPathRef.PICTURE_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + PICTURES_FOLDER;
                            //case SypPathRef.PICTURE_PAGE: return (SiteID == -1 || CategoryID == -1 || AlbumID == -1 || PictureID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/" + PICTURE_PREF + PictureID + XML_EXTENSION;
                            //case SypPathRef.ALBUM_PAGE: return (SiteID == -1 || CategoryID == -1 || AlbumID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/" + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.ALBUM_PAGE: return (AlbumID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            //case SypPathRef.ALBUM_FOLDER: return (SiteID == -1 || CategoryID == -1 || AlbumID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/";
                            //case SypPathRef.ALBUM_FOLDER: return (SiteID == -1 || CategoryID == -1 || AlbumID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/";
                            case SypPathRef.SYNC_FILE: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + SYNC_FILE;
                            case SypPathRef.IMAGE_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + IMAGES_FOLDER;
                            case SypPathRef.THUMBNAIL_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + THUMBNAILS_FOLDER;
                            case SypPathRef.TOUP_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + TO_UP_FOLDER;
                            case SypPathRef.ZIP_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + ZIP_FOLDER;
                            case SypPathRef.ZIP_FILE: return (SiteID == -1 || AlbumID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + ZIP_FOLDER + AlbumID + ZIP_EXTENSION;
                            case SypPathRef.JAVASCRIPT_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + JAVASCRIPT_FOLDER;
                            case SypPathRef.ROOT_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER;
                            case SypPathRef.STYLESHEET_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + STYLESHEET_FOLDER;
                            case SypPathRef.XSLT_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + XSLT_FOLDER;
                            case SypPathRef.SITE_FOLDER: return (SiteID == -1) ? null : TMP_FOLDER + SiteID + "/";
                            case SypPathRef.SITE_CONF: return (SiteID == -1) ? null : SITES_CONF + SiteID + XML_EXTENSION;
                            case SypPathRef.SITE_CONF_FOLDER: return SITES_CONF;
                            case SypPathRef.SITE_CONF_FILTER: return "*" + CONF_EXTENSION;
                            case SypPathRef.TMP_FOLDER: return TMP_FOLDER;
                            case SypPathRef.DESIGN_FOLDER: return DESIGN_FOLDER;
                            case SypPathRef.SKIN_THUMBNAIL_NAME: return SKIN_THUMBNAIL;
                            case SypPathRef.SKIN_THUMBNAIL_PATH: return DESIGN_FOLDER + design + "/" + SKIN_THUMBNAIL;
                            case SypPathRef.XSLT_PAGE: return TMP_FOLDER + SiteID + "/" + LOCAL_FOLDER + XSLT_FOLDER + XSLT_PAGE;
                            case SypPathRef.XSLT_PAGE_RELATIF : return "../" + XSLT_FOLDER + XSLT_PAGE;
                            default: return null;
                        }
                    }
                case SypPathMode.ONLINE:
                    {
                        switch (Ref)
                        {
                            case SypPathRef.THUMBNAIL_FILE: return (PictureID == -1) ? null : THUMBNAILS_FOLDER + THUMBNAIL_PREF + PictureID + THUMBNAIL_EXTENSION;
                            case SypPathRef.PICTURE_FILE: return (PictureID == -1) ? null : PICTURES_FOLDER + PICTURE_PREF + PictureID + PICTURE_EXTENSION;
                            //case SypPathRef.CATEGORY_FOLDER: return (CategoryID == -1) ? null : CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/";
                            case SypPathRef.CATEGORIES_FOLDER: return CATEGORIES_FOLDER;
                            case SypPathRef.INDEX_PAGE: return DEFAULT_PAGE;
                            case SypPathRef.BO_PAGE: return BO_PAGE;
                            case SypPathRef.PICTURE_FOLDER: return PICTURES_FOLDER;
                            //case SypPathRef.ALBUM_PAGE: return (CategoryID == -1 || AlbumID == -1) ? null : CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/" + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.ALBUM_PAGE: return (AlbumID == -1) ? null : CATEGORIES_FOLDER  + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            //case SypPathRef.PICTURE_PAGE: return (CategoryID == -1 || AlbumID == -1 || PictureID == -1) ? null : CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/" + PICTURE_PREF + PictureID + PAGE_EXTENSION;
                            //case SypPathRef.ALBUM_FOLDER: return (CategoryID == -1 || AlbumID == -1) ? null : CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/";
                            //case SypPathRef.ALBUM_FOLDER: return (CategoryID == -1 || AlbumID == -1) ? null : CATEGORIES_FOLDER + CATEGORY_PREF_DIR + CategoryID + "/";
                            case SypPathRef.THUMBNAIL_FOLDER: return THUMBNAILS_FOLDER;
                            case SypPathRef.ZIP_FOLDER: return ZIP_FOLDER;
                            case SypPathRef.ZIP_FILE: return ZIP_FOLDER + AlbumID + ZIP_EXTENSION;
                            case SypPathRef.IMAGE_FOLDER: return IMAGES_FOLDER;
                            case SypPathRef.JAVASCRIPT_FOLDER: return JAVASCRIPT_FOLDER;
                            case SypPathRef.STYLESHEET_FOLDER: return STYLESHEET_FOLDER;
                            case SypPathRef.XSLT_FOLDER: return XSLT_FOLDER;
                            case SypPathRef.TOUP_FOLDER: return TO_UP_FOLDER;
                            case SypPathRef.XSLT_PATH_THUMBNAIL: return (PictureID == -1) ? null : "../" + THUMBNAILS_FOLDER + THUMBNAIL_PREF + PictureID + THUMBNAIL_EXTENSION;
                            case SypPathRef.XSLT_PATH_PICTURE: return (PictureID == -1) ? null : "../" + PICTURES_FOLDER + PICTURE_PREF + PictureID + PICTURE_EXTENSION;
                            case SypPathRef.XSLT_ABOUT_LNK: return "../" + ABOUT_PAGE_INDEX;
                            case SypPathRef.XSLT_ARCHIVE_LNK: return (AlbumID == -1) ? null : "../../" + ZIP_FOLDER + AlbumID + ZIP_EXTENSION;
                            case SypPathRef.XSLT_PATH_STYLESHEET: return "../" + STYLESHEET_FOLDER;
                            case SypPathRef.XSLT_PATH_JAVASCRIPT: return "../" + JAVASCRIPT_FOLDER;
                            case SypPathRef.XSLT_PAGENAME_TYPE_0: return (AlbumID == -1)? null : ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_PAGENAME_TYPE_1: return (AlbumID == -1) ? null : ALBUM_PAGE_PREF + AlbumID + XML_EXTENSION;
                            //case SypPathRef.XSLT_PAGENAME_TYPE_1: return (PictureID == -1)?  null : PICTURE_PREF + PictureID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_ALBUM_CURRENT_LNK: return (AlbumID == -1) ? null : ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            //case SypPathRef.XSLT_ALBUM_LNK: return (CategoryID == -1 || AlbumID == -1) ? null : "../../" + CATEGORY_PREF_DIR + CategoryID + "/" + ALBUM_PREF_DIR + AlbumID + "/" + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_ALBUM_LNK: return (CategoryID == -1 || AlbumID == -1) ? null : "../" + ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_CONTACT_LNK: return "#";
                            case SypPathRef.XSLT_THUMBNAIL_LNK: return (PictureID == -1) ? null : PICTURE_PREF + PictureID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_PREV_NEXT_PICTURE_NAME: return (PictureID == -1) ? null : PICTURE_PREF + PictureID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_BACK_TO_ALBUM: return (AlbumID == -1) ? null : ALBUM_PAGE_PREF + AlbumID + PAGE_EXTENSION;
                            case SypPathRef.XSLT_FILE_PATH: return (CategoryID == -1 || AlbumID == -1) ? null : CATEGORY_PREF_DIR + CategoryID + "/";
                            case SypPathRef.ROOT_FOLDER: return "/";
                            
                            default: return null;
                        }
                    }
                case SypPathMode.DESIGN:
                    {
                        if (design != null)
                        {
                            switch (Ref)
                            {
                                case SypPathRef.ADMIN_IMAGE_FOLDER: return DESIGN_FOLDER + design + "/" + ADMIN_FOLDER + IMAGES_FOLDER;
                                case SypPathRef.ADMIN_JAVASCRIPT_FOLDER: return DESIGN_FOLDER + design + "/" + ADMIN_FOLDER + JAVASCRIPT_FOLDER;
                                case SypPathRef.ADMIN_STYLESHEET_FOLDER: return DESIGN_FOLDER + design + "/" + ADMIN_FOLDER + STYLESHEET_FOLDER;
                                case SypPathRef.ADMIN_XSLT_FOLDER: return DESIGN_FOLDER + design + "/" + ADMIN_FOLDER + XSLT_FOLDER;
                                case SypPathRef.ADMIN_STATIC_FOLDER: return DESIGN_FOLDER + design + "/" + ADMIN_FOLDER + STATIC_FOLDER;

                                case SypPathRef.XSLT_FOLDER: return DESIGN_FOLDER + design + "/" + XSLT_FOLDER;
                                case SypPathRef.IMAGE_FOLDER: return DESIGN_FOLDER + design + "/" + IMAGES_FOLDER;
                                case SypPathRef.JAVASCRIPT_FOLDER: return DESIGN_FOLDER + design + "/" + JAVASCRIPT_FOLDER;
                                case SypPathRef.STYLESHEET_FOLDER: return DESIGN_FOLDER + design + "/" + STYLESHEET_FOLDER;
                                
                                default: return null;
                            }

                        }
                        return null;
                    }
                default :
                    {
                        return null;
                    }
            }
        }
    }
}
